﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using QLKTX1._0.DAO;
using QLKTX1._0.DTO;


namespace QLKTX1._0.GUI.CauHinh
{
    public partial class FrmDotDangKy : Form
    {
       public int ds;

        public FrmDotDangKy()
        {
            InitializeComponent();
         
        }

        private void FrmDotDangKy_Load(object sender, EventArgs e)
        {
            button1.Enabled = false;
            this.ds = DotDangKyDAO.dsDotDangKy().Count;
            grvDotDangKy.DataSource = this.viewDanhSachDDK(DotDangKyDAO.dsDotDangKy());
           // grvDotDangKy.Columns[6].Visible = false;
           // textBox1.DataBindings.Clear();
           // textBox1.DataBindings.Add("Text", grvDotDangKy.DataSource, "maDot");
          //  textBox2.DataBindings.Clear();
           // textBox2.DataBindings.Add("Text", grvDotDangKy.DataSource, "dsHocKy");

            //grvDotDangKy.Dat

            //grvDotDangKy.DataBind();
            //checkedListBox1.DataBindings.Add
           // List<HocKyDTO> l = (List<HocKyDTO>)grvDotDangKy.SelectedRows[0].DataBoundItem;

           // textBox2.Text = l.Count.ToString();

          //  checkedListBox1.DataBindings.Clear();
           // checkedListBox1.DataBindings.Add("HocKyDTO", grvDotDangKy.DataSource, "dsHocKy");
        }

        private DataTable viewDanhSachDDK(List<DotDangKyDTO> ds)
        {
            DataTable dt = new DataTable();

            DataColumn col1 = new DataColumn("maDot");

            col1.DataType = System.Type.GetType("System.String");

            dt.Columns.Add(col1);

            DataColumn col2 = new DataColumn("tenDot");

            col2.DataType = System.Type.GetType("System.String");

            dt.Columns.Add(col2);

            DataColumn col3 = new DataColumn("ngayBD");

            col3.DataType = System.Type.GetType("System.DateTime");

            dt.Columns.Add(col3);

            DataColumn col4 = new DataColumn("ngayKT");

            col4.DataType = System.Type.GetType("System.DateTime");

            dt.Columns.Add(col4);            

            DataColumn col5 = new DataColumn("namHoc");

            col5.DataType = System.Type.GetType("System.String");

            dt.Columns.Add(col5);

            //dt.Columns.Add(new DataColumn("tenDot"));
          //  dt.Columns.Add(new DataColumn("ngayBD"));
            //dt.Columns.Add(new DataColumn("ngayKT"));
           // dt.Columns.Add(new DataColumn("namHoc"));
           
            //dt.Columns.Add(new DataColumn("dsHocKy"));


           

            foreach (DotDangKyDTO dot in ds)
            {
                dt.AcceptChanges();

                DataRow dr = dt.NewRow();

                dr[0] = dot.MaDot;

                dr[1] = dot.TenDot;

                dr[2] = dot.NgayBatDau;

                dr[3] = dot.NgayKetThuc;

                dr[4] = dot.MaNamHoc.TenNamHoc;                 
               

                dt.Rows.Add(dr);

            }            

            return dt;
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void grvDotDangKy_BindingContextChanged(object sender, EventArgs e)
        {          
        }

        private void bindingNavigator1_RefreshItems(object sender, EventArgs e)
        {

        }

        private void grvDotDangKy_SelectionChanged(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in grvDotDangKy.SelectedRows)
            {

               // DotDangKyDTO dst = row.DataBoundItem as DotDangKyDTO;       

                //dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value

                //DotDangKyDTO d = (DotDangKyDTO)grvDotDangKy.Rows[row.Index].Cells[row.Index].Value;
                //MessageBox.Show(d.MaDot.ToString());
                
                //...
            }
        }

        private void theemToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
            DataTable ds2 = (DataTable)grvDotDangKy.DataSource; // lay du lieu
           
            if (ds2.Rows.Count == this.ds)
            {

                int t = ds2.Rows.Count;

                DataRow dr = ds2.NewRow(); // them hang 

                dr[0] = Service.generateNoAuto("Dot_DangKy", "maDot", "D", 1);

                dr[1] = "222";

                // dr.set

                //  dr[3] = "222";

                // dr[4] = "222";

                ds2.Rows.InsertAt(dr, t + 1); // chen vao cuoi hang

                if ((ds2.Rows.Count - t) == 1)
                {
                    grvDotDangKy.DataSource = ds2; // cap nhat gridview
                }

                

            } else  MessageBox.Show("Ban da them 1 ban ghi nhung chua cap nhat ");
            

        }

        private void grvDotDangKy_UserAddedRow(object sender, DataGridViewRowEventArgs e)
        {
            button1.Enabled = true;
        }

        private void grvDotDangKy_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            //if (e.ColumnIndex == 1)
           // {
               
               //     e.CellStyle.BackColor = Color.Blue;
                
           // }           
        }
        // su kien xay ra khi thay dong
        private void grvDotDangKy_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            //button1.Enabled = true;
        }

        private void grvDotDangKy_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            foreach (DataGridViewRow row in grvDotDangKy.Rows)
            {
                if (Convert.ToInt32(row.Cells[4].Value) == 1)
                {
                    row.DefaultCellStyle.BackColor = Color.Red;
                }
                else if (Convert.ToInt32(row.Cells[4].Value) == 2)
                {
                    row.DefaultCellStyle.BackColor = Color.Green;
                }
                else
                {
                    row.DefaultCellStyle.BackColor = Color.Yellow;
                }

                // MessageBox.Show(row.Cells[4].Value.ToString());
            } 
        }
    }
}
